from aiogram import F
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import TextInput
from aiogram_dialog.widgets.kbd import Back, Button, Cancel, Group, ScrollingGroup, Select, SwitchTo
from aiogram_dialog.widgets.text import Format

from src.presentation.tg_bot.dialogs.client.common_handlers import add_order
from src.presentation.tg_bot.dialogs.client.common_windows import get_categories_window, get_countries_window
from src.presentation.tg_bot.dialogs.client.orders.orders_history.handlers import show_user_orders
from src.presentation.tg_bot.dialogs.getters.orders import order_summary_getter
from src.presentation.tg_bot.dialogs.getters.providers import providers_getter
from src.presentation.tg_bot.dialogs.getters.sites import sites_getter
from src.presentation.tg_bot.dialogs.widgets import LocaleText

# from src.presentation.tg_bot.dialogs.getters.proxies import available_proxy_amount_getter
from src.presentation.tg_bot.states import SelectProxySG

from .handlers import (
    back_to_provider,
    set_order_quantity,
    set_proxy_period,
    set_proxy_privacy,
    set_proxy_provider,
    set_proxy_type,
    set_site_id,
)

select_proxy_type_window = Window(
    LocaleText("select-proxy-type"),
    Group(
        # Button(LocaleText("server"), id="server", on_click=set_proxy_type),
        Button(LocaleText("mobile"), id="mobile", on_click=set_proxy_type),
        width=2,
    ),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.list_proxy_types,
)

select_proxy_privacy_window = Window(
    LocaleText("select-proxy-privacy"),
    Group(
        Button(LocaleText("private-proxy"), id="private", on_click=set_proxy_privacy),
        Button(LocaleText("shared-proxy"), id="shared", on_click=set_proxy_privacy),
        width=2,
    ),
    Back(LocaleText("back-btn")),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.select_proxy_privacy,
)

select_provider_window = Window(
    LocaleText("select-proxy-provider"),
    ScrollingGroup(
        Select(
            Format("{item.name}"),
            "catselsel",
            lambda provider: provider.id,
            "providers",
            on_click=set_proxy_provider,
        ),
        width=2,
        height=4,
        id="catsel",
        hide_on_single_page=True,
        when=F["user_language"] == "ru",
    ),
    ScrollingGroup(
        Select(
            Format("{item.name_eng}"),
            "catselsel",
            lambda provider: provider.id,
            "providers",
            on_click=set_proxy_provider,
        ),
        width=2,
        height=4,
        id="catsel",
        hide_on_single_page=True,
        when=F["user_language"] == "en",
    ),
    Back(LocaleText("back-btn")),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.select_provider,
    getter=providers_getter,
)

select_site_window = Window(
    LocaleText("select-site"),
    ScrollingGroup(
        Select(
            Format("{item.name}"),
            "catselsel",
            lambda site: site.id,
            "sites",
            on_click=set_site_id,
        ),
        width=2,
        height=4,
        id="catsel",
        hide_on_single_page=True,
    ),
    Back(LocaleText("back-btn")),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.select_site,
    getter=sites_getter,
)

period_window = Window(
    LocaleText("select-period"),
    Group(
        Button(LocaleText("four_hours"), id="four_hours", on_click=set_proxy_period),
        Button(LocaleText("one_day"), id="one_day", on_click=set_proxy_period),
        Button(LocaleText("one_week"), id="one_week", on_click=set_proxy_period),
        Button(LocaleText("two_weeks"), id="two_weeks", on_click=set_proxy_period),
        Button(LocaleText("one_month"), id="one_month", on_click=set_proxy_period),
        width=2,
    ),
    Back(LocaleText("back-btn"), when=F["dialog_data"]["proxy_privacy"] != "private"),
    Button(
        LocaleText("back-btn"),
        when=F["dialog_data"]["proxy_privacy"] == "private",
        id="back_btn",
        on_click=back_to_provider,
    ),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.select_period,
)

set_order_quantity_window = Window(
    LocaleText(
        "select-amount",
        available_amount="{available_amount}",
        when=F["available_amount"] > 0,
    ),
    LocaleText("no-proxies-available", when=F["available_amount"] <= 0),
    TextInput("orderquantityinp", int, on_success=set_order_quantity),
    Back(LocaleText("back-btn"), when=~F["for_test"]),
    SwitchTo(
        LocaleText("back-btn"),
        id="back_btn",
        when=F["for_test"],
        state=SelectProxySG.select_provider,
    ),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.set_order_quantity,
    # getter=available_proxy_amount_getter,
)

create_order_window = Window(
    LocaleText("order-overview"),
    LocaleText("proxy-type", proxy_type="{proxy_type}"),
    LocaleText("proxy-privacy", proxy_privacy="{proxy_privacy}"),
    LocaleText("proxy-country", country="{country}"),
    LocaleText("proxy-provider", provider="{provider}"),
    LocaleText("proxy-category", category="{category}"),
    LocaleText("proxy-site", site="{site}", when=F["site"]),
    LocaleText("selected-period", period="{period}"),
    LocaleText("selected-amount", amount="{selected_amount}"),
    LocaleText("order-summ", order_summ="{order_summ}$"),
    Button(
        LocaleText("confirm-btn"),
        id="confirm_order_button",
        on_click=add_order,
    ),
    Back(LocaleText("back-btn")),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.create_order,
    getter=order_summary_getter,
)

item_order_created_window = Window(
    LocaleText("order-created"),
    Button(LocaleText("orders-btn"), id="proxy", on_click=show_user_orders),
    Cancel(LocaleText("main-menu-btn")),
    state=SelectProxySG.order_created,
)


create_order_dialog = Dialog(
    select_proxy_type_window,
    select_proxy_privacy_window,
    get_countries_window(SelectProxySG),
    select_provider_window,
    get_categories_window(SelectProxySG),
    select_site_window,
    period_window,
    set_order_quantity_window,
    create_order_window,
    item_order_created_window,
)
